package com.zzw.atmoic;

import java.util.concurrent.atomic.AtomicReference;

public class SafeWM {
    static class WMRange{
        final int upper;
        final int lower;
        WMRange(int upper,int lower){
           this.upper = upper;
           this.lower = lower;
        }
    }
    final AtomicReference<WMRange>
            rf = new AtomicReference<>(
            new WMRange(0,0)
    );
    // 设置库存上限
    void setUpper(int v){
        WMRange nr;
        WMRange or = rf.get();
        do{
            // 检查参数合法性
            if(v < or.lower){
                throw new IllegalArgumentException();
            }
            nr = new
                    WMRange(v, or.lower);
        }while(!rf.compareAndSet(or, nr));
    }

    public static void main(String[] args) {
        WMRange wm = new WMRange(0, 0);
        AtomicReference rf = new AtomicReference<>(wm);
        WMRange wmRange = new WMRange(0, 0);
        System.out.println("" + rf.compareAndSet(wm, wmRange));
    }
}